﻿ /*************************************************************************
File:   as/rfid_vb.as
Author: Duykham
UpDate: 27/04/2009
TODO: Nhan bien RFID tu VB, ghi hinh, sau do request PHP de ban ra thumnail dang JPG
/*************************************************************************/
trace ("#included: RFID_VB.as");
var shoot_connection;
shoot_connection = new NetConnection ();
//shoot_connection.connect (_root.CFG.fmsApp);//--> khong connect o day vi chua load xong du lieu tu CFG --> see line 106!
//echo ("_root.CFG.fmsApp: " + _root.CFG.fmsApp);
//shoot_connection.connect ("rtmp:/ProconDemoApp/");
//var shoot_ns = new NetStream (shoot_connection);
var alarmFlag;
//var URL_txt;
var cnStatus;
var nsStatus;
var rfid = "0";
var boardid = "0";
// RFID do Vb truyen sang
var my_duration = 0;
var frm_duration;
var my_alert = "";
var my_course = 0;
var my_device = "";
//var my_ip;
var shoot_ns;
var folderName;
var fileName;
var i = 0;
var process_lv = new LoadVars ();
var policy_lv = new LoadVars ();
var checkip_lv = new LoadVars ();
var dangGhi : Boolean = false;
var gotRfid: Boolean = false;
var myMotionLevel = 20;
trace("myMotionLevel: "+myMotionLevel);
var this_io=1;
var fsSent=false;
var shootCam = Camera.get (_root.CFG.rfidCam);
shootCam.setMotionLevel (20, 2000);
shootCam.setMode(320,240,30);
shootCam.setQuality(0, 100);
function shoot (card_id)
{
	echo ("function shoot(" + card_id + ")");
	fscommand ("", "shoting ID: " + card_id);
	shoot_btn.enabled = false;
	//var shootCam = Camera.get (_root.CFG.rfidCam);
	
	shoot_ns = new NetStream (shoot_connection);
	shoot_ns.attachVideo (shootCam);
	
	folderName = time_mc.strDate;
	fileName = time_mc.gio24 + "-" + time_mc.phut + "-" + time_mc.giay + "." + card_id;
	//shoot_ns.publish ("rfid/" + time_mc.strDate + "/" + time_mc.gio24 + "-" + time_mc.phut + "-" + time_mc.giay + "." + card_id, "record");
	shoot_ns.publish ("rfid/" + my_course + "/" + folderName + "/" + fileName, "record");
	trace ("rfid/" + time_mc.strDate + "/" + time_mc.gio24 + "-" + time_mc.phut + "-" + time_mc.giay + "." + card_id);
	//shoot_ns.publish ("rfid/21-04-2007/08-15-01." + card_id, "record");
	//while (_global.giay <= second)
	//{
	//if (_global.giay = second)
	//{
	//shoot_ns.publish(false);
	//shoot_ns.close();
	//}
	//}
	//}
	rfid = "0"; 	//20090422
	i = 0;			//
	dangGhi = true;	//
};
shootCam.onActivity = function(isActive:Boolean)
{
	io=(isActive)?0:1;
	fsSent=false;
}
/*
//TEST
shoot_btn.onRelease = function ()
{
trace ("_global.gio=" + time_mc.gio24);
trace ("_root.CFG.fmsApp: "+_root.CFG.fmsApp);
_global.rfid = "1131465655";
}
*/

//check status of connection
shoot_connection.onStatus = function(info) {
	if(info.code == "NetConnection.Connect.Success") {
		cnStatus = "success";
	}
	else {
        cnStatus = info.code;
 	}
	echo ("cnStatus: "+info.code);
}

//check status of netStream
shoot_ns.onStatus = function (infoObj)
{
	if (infoObj.code == "NetStream.Publish.BadName")
	{
		nsStatus= "Someone is already recording to this stream";
	} else if (infoObj.code == "NetStream.Record.NoAccess")
	{
		nsStatus= "Someone is playing this stream";
	} else
	{
		nsStatus= "success";
	}
	echo("nsStatus: "+infoObj.code);
}
//Load duration, course, alertString from policy.php
policy_lv.onLoad = function (success)
{
	if (success)
	{
		//trace ("duration: "+policy_lv.duration);
		my_duration = policy_lv.duration;
		my_course = policy_lv.course;
		//URL_txt = my_duration;
		my_alert = policy_lv.alert;
		my_device = policy_lv.device;
		//trace ("my_duration: " + my_duration);
		//trace ("my_course: " + my_course);
		//trace ("my_alert: " + my_alert);
		if(cnStatus != "success")
		{
			echo ("Not connected! Start connecting to: " + _root.CFG.fmsApp);
			shoot_connection.connect (_root.CFG.fmsApp);//connect o day!!!
		}
		if (!dangGhi)
		{
			startShot();
		}
	}
	else
	{
		echo("can not connect to policy.php!!");
	}
};

process_lv.onLoad = function (success)
{
	if(success)
	{
		my_duration = 0;
		gotRfid = false;
		fscommand ("", "endShot");
	}
	else
	{
		echo ("cannot connect to FFMPEG and/or Database!!");
	}
}

function startShot()
{
	//var second = time_mc.giay + my_duration;
			fscommand(my_device,my_alert);			//truyen device va alert cho Vb xu ly!
			frm_duration = (my_duration!=undefined)? (my_duration * 12) : 120;
			if (cnStatus == "success") 
			{
				if(rfid != "0")
				{
					fscommand ("", "startShot");
					shoot (rfid);
					//rfid = "0"; --> dua vao trong ham shoot() (update:20090422)
					//gotRfid = false;
					//my_duration = 0; --> khong set o day voi!!!
					/*i = 0;
					dangGhi = true;*///--> dua vao trong ham shoot() (update:20090422)
				}
				else
				{
					echo ("Gia tri RFID khong hop le! RFID="+rfid);
				}
			}
			else
			{
				echo ("Connection fail: "+cnStatus);
			}
}

function stopShot()
{
		shoot_ns.publish (false);
		shoot_ns.close ();
		//shoot_connection.connect(null);
		shoot_btn.enabled = true;
		i = 0;
		dangGhi = false;
		process_lv.load (_root.CFG.rfidAdd + "/ffmpeg/process.php?date=" + folderName + "&filename=" + fileName + "&duration=" + my_duration + "&course=" + my_course + "&io=" + this_io);
		//URL_txt =  (_root.CFG.rfidAdd + "/ffmpeg/process.php?date=" + folderName + "&filename=" + fileName + "&duration=" + my_duration + "&course=" + my_course);
		//my_duration = 0;
		//fscommand ("", "endShot");
}

function getRfid()
{
	echo("RFID string: "+rfid);
	var idArray:Array = rfid.split("|");
	boardid = idArray[0];
	rfid = idArray[1];
	echo("RFID: "+rfid);
	gotRfid = true;
}
/*khong check IP nua!
//Load IP from checkip.php
checkip_lv.onLoad = function (success)
{
	if (success)
	{
		my_ip = checkip_lv.myip;
		trace ("my_ip: " + my_ip);
		policy_lv.load(_root.CFG.rfidAdd+"/policy.php?rfid="+rfid+"&ip="+my_ip);
	}
}
*/
//on enter frame
this.onEnterFrame = function ()
{
	if ((rfid != "0") && (dangGhi == false))
	{
		/*if(cnStatus != "success")
		{
            echo("connection status: " + cnStatus);
			echo ("connect again to: " + _root.CFG.fmsApp);
		   	shoot_connection.connect (_root.CFG.fmsApp);//connect o day!!!
        }*/
		if (_root.io==0)
		{
			if(fsSent==false)
			{
				this_io=_root.io;
				echo("OUT:"+shootCam.activityLevel);
				fsSent=true;
				//_root.io=2;
			}
		}
		else if (_root.io==1)
		{
			if(fsSent==false)
			{
				this_io=_root.io;
				echo ("IN");
				fsSent=true;
				//_root.io=2;
			}
		}
		if (!gotRfid)
		{
			getRfid();
		}
		/*if (cnStatus != "success")
		{
            echo("connection status: " + cnStatus);
			echo ("connect again to: " + _root.CFG.fmsApp);
		   	shoot_connection.connect (_root.CFG.fmsApp);//connect o day!!!
        }
		*/
		if (my_duration == 0)
		{
			//request policy.php de nhan ve gia tri cua duration
			//checkip_lv.load (_root.CFG.rfidAdd + "/checkip.php");
			policy_lv.load(_root.CFG.rfidAdd+"/policy.php?rfid="+rfid+"&roomid="+boardid);
		} 
	}
	if ((i == frm_duration) && (dangGhi == true))
	{
		stopShot();
	}
	i ++;
	alarmFlag = (time_mc.alarmFlg) ? "ON" : "OFF";
	//alarmFlag = "startH: "+time_mc.startH+" startM: "+time_mc.startM;
};
